/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Connect;
import Pojo.User;
import Util.HibernateUtil;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.management.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Tan Le Anh
 */
public class UserDAO {
    Connect cn;
    public static List<User> View()
    {
        List<User> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql = "select u from User u where u.loai!=5";
            org.hibernate.Query qr= se.createQuery(sql);
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            System.err.print(e);
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static List<User> ViewCus()
    {
        List<User> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql = "select u from User u where u.loai=5";
            org.hibernate.Query qr= se.createQuery(sql);
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            System.err.print(e);
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static List<User> Search(String name)
    {
        List<User> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql="select u from User u where u.loai!=5 and (u.tennguoidung like :name or u.taikhoan like :name)";
            org.hibernate.Query qr= se.createQuery(sql);
            qr.setString("name", "%"+name+"%");
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static List<User> SearchCus(String name)
    {
        List<User> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql="select u from User u where u.tennguoidung like :name and u.loai=5";
            org.hibernate.Query qr= se.createQuery(sql);
            qr.setString("name", "%"+name+"%");
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static User GetInfo(String taikhoan)
    {
        User u=null;
        Session se = HibernateUtil.getSessionFactory().openSession();
        try
        {
            u=(User) se.get(User.class, taikhoan);
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return u;
    }
    
    public static boolean Delete(String taikhoan)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();
        User u=UserDAO.GetInfo(taikhoan);
        if(u==null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.delete(u);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }   
    
    public static boolean Update(User u)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();        
        if(UserDAO.GetInfo(u.getTaikhoan()) ==null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.update(u);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    
    public static boolean Insert(User u)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();        
        if(UserDAO.GetInfo(u.getTaikhoan()) !=null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.save(u);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    
    public static boolean Login(String username, String password)
    {        
        List<User> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql = "from User u where u.taikhoan=:username and u.matkhau=:password";
            org.hibernate.Query qr= se.createQuery(sql);
            qr.setString("username", username);
            qr.setString("password", password);
            list=qr.list();
            if(list.size()==0)
            {
                return false;
            }
        }
        catch (HibernateException e)
        {
            System.err.print(e);
        }
        finally
        {
            se.close();
        }
        return true;
    }
}
